package com.naiterui.ehp.bs.patient.controller.inner;

import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.patient.PersonalDoctorBO;
import com.naiterui.ehp.bp.bo.patient.RevisitDoctorBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientDoctorService;
import com.naiterui.ehp.bs.patient.utils.exception.PatientBusinessException;
import com.naiterui.ehp.bs.patient.vo.doctor.DoctorVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 患者关注医生inner接口
 *
 * @since 1.0.0
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.USER_INFO})
@RequestMapping("ap/inner/personal/doctor")
@Controller
public class ApInnerPatientDoctorController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApInnerPatientDoctorController.class);

    @Autowired
    private IPatientDoctorService patientService;

    /**
     * 获取关注医生总数
     * InnerPatientDoctorController.count()
     *
     * @param patientId
     * @return
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    @RequestMapping("list")
    @ResponseBody
    public ResponseVO<List<PersonalDoctorBO>> count(Long patientId) {
        if (patientId == null) {
            LOGGER.error("参数错误，patientId", patientId);
            return PublicService.returnResponseVO(new ArrayList<PersonalDoctorBO>());
        }
        PageVO<DoctorVO> doctorPage = patientService.getPrivateDoctorList(patientId, 0, 0);
        List<PersonalDoctorBO> doctorList = new ArrayList<>();
        List<DoctorVO> voList = doctorPage.getResult();
        PersonalDoctorBO bo = null;
        for (DoctorVO vo : voList) {
            bo = new PersonalDoctorBO();
            bo.setName(vo.getName());
            bo.setDoctorId(vo.getDoctorId());
            doctorList.add(bo);
        }
        return PublicService.returnResponseVO(doctorList);
    }

    /**
     * 添加私人医生内部接口
     * InnerPatientDoctorController.add()
     *
     * @param patientId
     * @param doctorId
     * @param firstRecom
     * @param source     0:公众号,1:互联网医院,2:推荐咨询活动,3:患者找医生
     * @return
     * @throws Exception
     * @Date 2018年9月6日
     * @since 2.24
     */
    @RequestMapping("add")
    @ResponseBody
    @ApiOperation(value = "添加私人医生内部接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "source", value = "医生来源：0:公众号,1:互联网医院,2:推荐咨询活动,3:患者找医生, 4:微信小程序", required = true, dataType = "long",
            paramType = "query")})
    public ResponseVO<Void> add(Long patientId, Long doctorId, Integer firstRecom, Integer source) throws Exception {
        if (patientId == null || doctorId == null || source == null) {
            LOGGER.error("添加私人医生内部接口 参数错误，patientId: {},doctorId: {},firstRecom: {},source: {}", patientId, doctorId, firstRecom, source);
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (firstRecom == null) {
            firstRecom = 0;
        }
        patientService.addPersonalDoctor(doctorId, patientId, firstRecom, source);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

    @RequestMapping("revisit")
    @ResponseBody
    public ResponseVO<RevisitDoctorBO> add(Long patientId, Long doctorId) {
        RevisitDoctorBO revisitDoctorBO = patientService.getRevisitDoctor(patientId, doctorId);
        return PublicService.returnResponseVO(revisitDoctorBO);
    }

    /**
     * 根据医生id获取医生主页信息
     * 医生端主页信息内部调用
     * @param doctorId
     * @param patientId
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "mineInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据医生ID获取医生主页信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<DoctorVO> mineInfo(Long doctorId, Long patientId) throws BusinessException {
        LOGGER.info("根据医生id获取医生主页信息，医生端主页信息内部调用 doctorId={}",doctorId);
        DoctorVO doctorVO = patientService.findDoctorDetail(doctorId, patientId);
        return PublicService.returnResponseVO(doctorVO);
    }
}
